package com.llt.ai.controller;

import lombok.RequiredArgsConstructor;
import org.springframework.ai.chat.client.ChatClient;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;
import reactor.core.publisher.Flux;

import java.util.List;

/**
 * @Author: llt
 * @Date: 2025/7/4 16:41
 */
@RequiredArgsConstructor
@RestController
@RequestMapping("/ai")
public class ChatController {

    private final ChatClient chatClient;

    /**
     * 如果不写 produces = "text/html;charset=utf-8", 大模型输出会乱码
     */
    @GetMapping(value = "/chat", produces = "text/html;charset=utf-8")
    public Flux<String> chat(String prompt) {
        return chatClient.prompt()
                .user(prompt)
                .stream()
                .content();
    }

    /**
     * 返回实体
     */
    record ActorFilms(String actor, List<String> movies) {
    }

    @GetMapping( "/entity")
    public ActorFilms entity(String input) {
        return chatClient.prompt()
                .user(input)
                .call()
                .entity(ActorFilms.class);
    }
}
